package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.dao.LiveInfoMapper;
import com.example.blog.dao.LiveUserMapper;
import com.example.blog.dto.*;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.entity.LiveCompany;
import com.example.blog.entity.LiveInfo;
import com.example.blog.entity.LiveUser;
import com.example.blog.response.CommonException;
import com.example.blog.service.CommunMemberService;
import com.example.blog.service.LiveInfoService;
import com.example.blog.service.LiveUserService;
import com.example.blog.util.ParamUtil;
import com.example.blog.util.RequestUtil;
import com.example.blog.util.ToolsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author Admin
 */
@Service
public class LiveInfoServiceImpl extends ServiceImpl<LiveInfoMapper, LiveInfo> implements LiveInfoService {

    @Resource
    private LiveInfoMapper baseMapper;

    @Resource
    private CommunMemberService communMemberService;

    @Override
    @Transactional
    public Boolean addLiveInfo(AddLiveInfoReqDto reqDto){
        if(ParamUtil.empty(reqDto.getRoomNo())){
            throw new CommonException("房间号不能为空");
        }
        if(ParamUtil.empty(reqDto.getPicUrl())){
            throw new CommonException("图片不能为空");
        }
        if(ParamUtil.empty(reqDto.getStartTime())){
            throw new CommonException("开始时间不能为空");
        }
        LiveInfo liveInfo = ToolsUtil.convertType(reqDto,LiveInfo.class);
        liveInfo.setId(ToolsUtil.getUUID());
        liveInfo.setUserId(reqDto.getUserId());
        return baseMapper.insert(liveInfo) > 0;
    }

    @Override
    @Transactional
    public Boolean updateLiveInfo(UpdateLiveInfoReqDto reqDto){
        LiveInfo liveInfo = ToolsUtil.convertType(reqDto,LiveInfo.class);
        return baseMapper.updateById(liveInfo) > 0;
    }

    @Override
    @Transactional
    public Boolean deleteLiveInfo(IdRequestDto requestDto){
        LiveInfo liveInfo = baseMapper.selectById(requestDto.getId());
        //软删除
        liveInfo.setStatus(9);
        return baseMapper.updateById(liveInfo) > 0;
    }

    @Override
    @Transactional
    public IPage<QueryLiveInfoResDto> selectLiveInfo(QueryLiveInfoReqDto reqDto){
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<LiveInfo> wrapper = Wrappers.lambdaQuery(LiveInfo.class);
        if(ParamUtil.notEmpty(reqDto.getRoomNo())){
            wrapper.like(LiveInfo::getRoomNo,reqDto.getRoomNo());
        }
        if(ParamUtil.notEmpty(reqDto.getRoomName())){
            wrapper.like(LiveInfo::getRoomName,reqDto.getRoomName());
        }
        if(ParamUtil.notEmpty(reqDto.getLiveStatus())){
            wrapper.eq(LiveInfo::getLiveStatus,reqDto.getLiveStatus());
        }
        Page<LiveInfo> liveInfoIPage = baseMapper.selectPage(reqDto.iPageInfo(), wrapper.ne(LiveInfo::getStatus,9)
                .orderByDesc(LiveInfo::getStartTime));
        IPage<QueryLiveInfoResDto> queryLiveInfoResDtoPage = ToolsUtil.convertType(liveInfoIPage,QueryLiveInfoResDto.class);
        return queryLiveInfoResDtoPage;
    }

    @Override
    @Transactional
    public LiveInfo selectLiveInfoById(String id){
        return baseMapper.selectOne(Wrappers.lambdaQuery(LiveInfo.class).eq(LiveInfo::getId,id).ne(LiveInfo::getStatus,9));
    }

    @Override
    @Transactional
    public Boolean closeLive(CloseLiveReqDto reqDto){
        LiveInfo liveInfo = baseMapper.selectById(reqDto.getId());
        if(ParamUtil.empty(liveInfo)){
            throw new CommonException("未查询到直播信息");
        }
        if(ParamUtil.empty(reqDto.getPlaybackUrl())){
            throw new CommonException("回放链接不能为空");
        }
        communMemberService.changeOnline(reqDto);
        liveInfo.setEndTime(LocalDateTime.now());
        liveInfo.setLiveStatus(2);
        liveInfo.setPlaybackUrl(reqDto.getPlaybackUrl());
        return baseMapper.updateById(liveInfo) > 0;
    }

}
